package cn.edu.ctbu.eol.teacher.controller;

import cn.edu.ctbu.eol.teacher.common.CustomException;
import cn.edu.ctbu.eol.teacher.common.Result;
import cn.edu.ctbu.eol.teacher.pojo.Teacher;
import cn.edu.ctbu.eol.teacher.service.TeacherService;
import cn.edu.ctbu.eol.teacher.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/api")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("/getTeachers")
    public Map<String, Object> getTeachers(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String employeeNumber,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        return teacherService.getTeachersByPage(name, employeeNumber, page, limit);
    }

    @PostMapping("/addTeacher")
    public Map<String, Object> addTeacher(@RequestBody Teacher teacher) {
        Map<String, Object> result = new HashMap<>();
        boolean success = teacherService.addTeacher(teacher);
        result.put("code", success ? 0 : 1);
        result.put("msg", success ? "添加成功" : "添加失败");
        return result;
    }

    @PostMapping("/updateTeacher")
    public Map<String, Object> updateTeacher(@RequestBody Teacher teacher) {
        Map<String, Object> result = new HashMap<>();
        boolean success = teacherService.updateTeacher(teacher);
        result.put("code", success ? 0 : 1);
        result.put("msg", success ? "修改成功" : "修改失败");
        return result;
    }

    @PostMapping("/deleteTeacher")
    public Map<String, Object> deleteTeacher(@RequestBody Map<String, Object> requestBody) {
        Map<String, Object> result = new HashMap<>();
        Integer id = (Integer) requestBody.get("id");
        if (id == null) {
            result.put("code", 1);
            result.put("msg", "ID is required");
            return result;
        }
        boolean success = teacherService.deleteTeacher(id);
        result.put("code", success ? 0 : 1);
        result.put("msg", success ? "删除成功" : "删除失败");
        return result;
    }

    @PostMapping("/login")
    public Result<Map<String, String>> login(@RequestBody Teacher teacher) {
        String password = DigestUtils.md5DigestAsHex(teacher.getPassword().getBytes(StandardCharsets.UTF_8));

        Teacher dbTeacher = teacherService.getByEmployeeNumber(teacher.getEmployeeNumber());
        if (dbTeacher == null) {
            throw new CustomException("用户不存在");
        }

        if (!password.equals(dbTeacher.getPassword())) {
            throw new CustomException("密码错误");
        }

        String token = jwtUtil.generateToken(dbTeacher.getEmployeeNumber());

        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        return Result.success(map);
    }
}